lobsterlayui.main(['index', 'layer', 'form', 'element', 'table', 'tree'], {
    data: {
        $layui: null,
        tableins: null,
        selectNode: { cid: '', pid: '', lv: 1, exactQuery: false }
    },
    pageload: function (right) {
        console.log(right);
        this.initevent();
        this.treeLoad();
    },
    initevent() {
        var self = this;
        layui.form.on('checkbox(exactQueryfilter)', function (data) {
            self.data.selectNode.exactQuery = data.elem.checked;
            //console.log(selectNode.exactQuery);
        });
        

        layui.form.on("submit(LAY-app-front-search)", function (data) {
            self.data.tableins.reload({
                where: {
                    cid: self.data.selectNode.cid,
                    pid: self.data.selectNode.pid,
                    lv: self.data.selectNode.lv,
                    drugName: $("#pName").val(),
                    dosagesName: $("#pDosage").val(),
                    exactQuery: self.data.selectNode.exactQuery
                },
                page: {
                    curr: 1
                }
            });
        });
    },

    //获取树结构
    treeLoad() {
        var self = this;
        lobsterlayui.GETAPI('tsgbdrug.gettree').then(retdata=>{
            self.viewToTree(retdata);
        })
    },

    //树结构加载
    viewToTree(treelist) {
        var self = this;

        layui.tree.render({
            elem: '#Left',
            isJump: true,
            onlyIconControl: true,
            autoRefresh: true,
            customOperate: true,
            data: treelist,
            edit: [], //操作节点的图标
            operate: function (obj) {
                var type = obj.type; //得到操作类型：
                var data = obj.data; //得到当前节点的数据
                var elem = obj.elem; //得到当前节点元素
                var id = data.id;
                var name = data.title;
            },
            click: function (obj) {
                var data = obj.data;

                $("#Left .nodeover").removeClass("nodeover");
                $(obj.elem).find(".layui-tree-main").eq(0).addClass("nodeover");
                self.data.selectNode.cid = data.id;
                self.data.selectNode.pid = data.parentid;
                self.data.selectNode.lv = data.level;
                self.TableLoad();
            }
        });
    },

    //类型列表加载
    TableLoad() {
        var self = this;

        var namestr = $("#pName").val();
        var dosagestr = $("#pDosage").val();
        self.data.tableins = layui.table.render({
            elem: "#entlist",
            headers: lobsterlayui.GetHeaderData(),
            where: {
                cid: self.data.selectNode.cid,
                pid: self.data.selectNode.pid,
                lv: self.data.selectNode.lv,
                drugName: namestr,
                dosagesName: dosagestr,
                exactQuery: self.data.selectNode.exactQuery
            },
            url: lobsterlayui.GetLowCodeUrl('tsgbdrug.getDrugdata'),
            defaultToolbar: [],
            //height: "full-70",
            size: '10',
            page: true, //开启分页
            //even: true,
            loading: true,
            cols: [
                [
                    { field: '药品ID', title: '药品ID', width: '5%' },
                    { field: '品种名称', title: '品种名称', width: '25%' },
                    { field: '药品名称', title: '药品名称', width: '20%' },
                    { field: '规格', title: '规格', width: '10%' },
                    { field: '剂型', title: '剂型', width: '10%' },
                    { field: '类型', title: '类型', width: '20%' },
                    { field: '处方资质', title: '处方资质', width: '10%' }
                ]
            ],
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.List
                }
            }
        });
    }
})